const db = wx.cloud.database();
const app = getApp();
const config = require("../../config.js");
Page({

      /**
       * 页面的初始数据
       */
      data: {
            showShare: false,
            poster: [JSON.parse(config.data).share_poster],
            openid: '',
      },
      onLoad() {
            this.find()
      },
      onShow() {
            this.setData({
                  userinfo: app.userinfo
            })
            console.log(app.userinfo)
            console.log(app.openid)
      },
      find() {
            let that = this;
            if(!app.openid){
                  wx.cloud.callFunction({
                        name: "login",
                  }).then((res) => {
                        that.setData({
                        openid : res.result.openid      
                        })
                  })
                  console.log(that.data.openid)
            } else {
                  db.collection("user").where({
                        _openid: app.openid
                  }).get({
                        success(res) {
                              console.log(res.data)
                              if (res.data.length != 0) {
                                    console.log("搜到了")
                                    that.setData({
                                          userinfo:1
                                    })
                                    app.userinfo = res.data[0];                                
                                    app.openid = res.data[0]._openid;
                              } else {
                                    wx.navigateTo({
                                          url: '/pages/login/login'
                                    })
                              }
                        }
                  })
            }

      },
      
      go(e) {
            if (e.currentTarget.dataset.status == '1') {
                  if (!app.openid) {
                        wx.showModal({
                              title: '温馨提示',
                              content: '该功能需要注册方可使用，是否马上去注册',
                              success(res) {
                                    if (res.confirm) {
                                          wx.navigateTo({
                                                url: '/pages/login/login',
                                          })
                                    }
                              }
                        })
                        return false
                  }
            }
            wx.navigateTo({
                  url: e.currentTarget.dataset.go
            })
      },
      //展示分享弹窗
      showShare() {
            this.setData({
                  showShare: true
            });
      },
      //关闭弹窗
      closePop() {
            this.setData({
                  showShare: false,
            });
      },
      //预览图片
      preview(e) {
            console.log(e),
                  wx.previewImage({
                        urls: e.currentTarget.dataset.link
                  });
      },
      onShareAppMessage() {
            return {
                  title: JSON.parse(config.data).share_title,
                  imageUrl: JSON.parse(config.data).share_img,
                  path: '/pages/start/start'
            }

      },
})